package com.changcheng.project.android.controller;

import com.changcheng.framework.aspectj.lang.annotation.Log;
import com.changcheng.framework.aspectj.lang.enums.BusinessType;
import com.changcheng.project.android.domain.APP;
import com.changcheng.project.android.domain.APP_Updata;
import com.changcheng.project.android.domain.AndroidBean;

import com.changcheng.project.apppublish.domain.AAppPublish;
import com.changcheng.project.apppublish.service.IAAppPublishService;
import com.changcheng.project.appupgradelog.domain.AAppUpgradelogs;
import com.changcheng.project.appupgradelog.service.IAAppUpgradelogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(description = "App升级")
@RestController
@RequestMapping("/etcAfterSale")
@SuppressWarnings("all")
public class AndroidAPP {
    @Autowired
    private IAAppUpgradelogsService aAppUpgradelogsService;
    @Autowired
    private IAAppPublishService aAppPublishService;

    /**
     * 查询app是否需要升级 0是失败  1是成功
     */
    @ApiOperation(value = "app升级查询", notes = "app升级查询")
    @PostMapping("/appUpdateQuery")
    public AndroidBean appUpdateQuery(@RequestBody APP_Updata app_updata) {
        System.out.println(app_updata);
        AndroidBean androidBean = new AndroidBean();

        //当前app的版本
        String appversion = app_updata.getCurrentVersion();
        //手持终端号
        String deviceId = app_updata.getDeviceId();

        List<AAppPublish> list = aAppPublishService.selectAAppPublishList(null);
        if (list != null && list.size() > 0) {
            //有厂家的数据
            AAppPublish aappPublish = list.get(0);
            String pingtaiversion = aappPublish.getAppPublishVersion();
            if (!pingtaiversion.equalsIgnoreCase(appversion)) {
                //根据手持机端号查询升级列表
                AAppUpgradelogs aAppUpgradelogs = aAppUpgradelogsService.selectAAppUpgradelogsByDeviceId(deviceId);
                AAppUpgradelogs aappUpgradelogs = new AAppUpgradelogs();
                //app日志表中查询的版本
                if (aAppUpgradelogs == null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy‐MM‐dd a hh:mm:ss");
                    aappUpgradelogs.setStartUpgradeTime(sdf.format(new Date()));
                    //当前版本
                    aappUpgradelogs.setEndUpgradeTime(app_updata.getCurrentVersion());
                    aappUpgradelogs.setSerStaName(app_updata.getStationName());
                    aappUpgradelogs.setAccountName(app_updata.getOperName());
                    aappUpgradelogs.setTermNumber(app_updata.getDeviceId());
                    aappUpgradelogs.setUpgradeStatus(null);
                    aAppUpgradelogsService.insertAAppUpgradelogs(aappUpgradelogs);
                } else {
                    String version = aAppUpgradelogs.getEndUpgradeTime();
                    if (appversion.equals(version)) {
                        aappUpgradelogs.setUpgradeStatus("失败");
                        aappUpgradelogs.setTermNumber(app_updata.getDeviceId());
                        aAppUpgradelogsService.updateAAppUpgradelogs1(aappUpgradelogs);
                    } else {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy‐MM‐dd a hh:mm:ss");
                        aappUpgradelogs.setStartUpgradeTime(sdf.format(new Date()));
                        aappUpgradelogs.setTermNumber(app_updata.getDeviceId());
                        aappUpgradelogs.setUpgradeStatus("成功");
                        aappUpgradelogs.setTermNumber(app_updata.getDeviceId());
//                        aappUpgradelogs.setEndUpgradeTime(pingtaiversion);
                        aAppUpgradelogsService.updateAAppUpgradelogs1(aappUpgradelogs);
                    }
                }

                //有升级数据
                androidBean.setCode(0);
                androidBean.setMessage("查询成功，有最新程序，请下载！");
                APP app = new APP();
                app.setIsNeedUpdate("0");
                app.setNewVersion(aappPublish.getAppPublishVersion());
                String url = aappPublish.getAppPublishUrl();
                url = url.substring(url.indexOf("/pro"), url.length());
                app.setUpdateUrl(url);
                androidBean.setObject(app);

            } else {
                //无升级数据
                androidBean.setCode(0);
                androidBean.setMessage("查询成功，app程序已是最新，暂无升级！");
                APP app = new APP();
                app.setIsNeedUpdate("1");
                androidBean.setObject(app);
            }

        } else {
            androidBean.setCode(0);
            androidBean.setMessage("查询成功，暂无发布数据！");
            APP app = new APP();
            app.setIsNeedUpdate("1");
            androidBean.setObject(app);
        }
        return androidBean;

    }

    /**
     * 新增保存app升级记录
     */

    @Log(title = "app升级记录", businessType = BusinessType.INSERT)
    @PostMapping("/appUpgradeResult")
    @ResponseBody
    public AndroidBean addSave(@RequestBody AAppUpgradelogs aappUpgradelogs) {
        AndroidBean androidBean = new AndroidBean();

        try {
            int rows = aAppUpgradelogsService.insertAAppUpgradelogs(aappUpgradelogs);
            if (rows > 0) {
                androidBean.setCode(1);
                androidBean.setMessage("上传成功！");
            } else {
                androidBean.setCode(0);
                androidBean.setMessage("上传失败！");
            }
        } catch (Exception e) {
            androidBean.setCode(0);
            androidBean.setMessage("上传失败！" + e);
        }
        return androidBean;
    }
}
